package com.wb.utils

import com.wb.component.Slf4j
import org.apache.http.protocol.HTTP
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import java.io.InputStream
import java.net.URL
import kotlin.jvm.Throws

/**
 * 动漫壁纸工具
 *
 * @author 祁雪
 */
@Slf4j
class AnimeUtil {

    companion object {

        /**
         * 普通: https://anime-pictures.net/pictures/view_posts/0?lang=zh_CN
         * 搜索: https://anime-pictures.net/pictures/view_posts/0?search_tag=%E5%8E%9F%E7%A5%9E&order_by=date&ldate=0&lang=zh_CN
         */
        val AP_URL = "https://anime-pictures.net/pictures/view_posts/";

        val PREFIX_URL = "https://anime-pictures.net";

        /**
         * 随机页码获取一张图片
         */
        @kotlin.jvm.Throws(Exception::class)
        fun getAP(searchKey: String?): InputStream? {

            var url = ""

            if (searchKey.isNullOrEmpty()) {
                url = "${AP_URL}${(Math.random() * 500).toInt()}?lang=zh_CN"
            } else {
                url = "${AP_URL}0?search_tag=${searchKey}&order_by=date&ldate=0&lang=zh_CN"
            }

            println("开始访问: $url")

            val result =
                RestUtil.sendGet(
                    url, null,
                    hashMapOf<String, String>(
                        "User-Agent" to "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.30 Safari/537.36",
                        "Content-Type" to "application/x-www-form-urlencoded",
                        "accept" to "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
//                        "cookie" to "_ga=GA1.2.950209228.1636860892; sitelang=zh_CN; _gid=GA1.2.1785797236.1637397412; _gat_gtag_UA_1465472_1=1",
                        "user-agent" to " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53",
                        "sec-ch-ua" to " \"Microsoft Edge\";v=\"95\", \"Chromium\";v=\"95\", \";Not A Brand\";v=\"99\"",
                        "sec-ch-ua-mobile" to "?0",
                        "sec-ch-ua-platform" to "\"Windows\"",
                        "sec-fetch-dest" to "document",
                        "sec-fetch-mode" to "navigate",
                        "sec-fetch-site" to "none",
                        "sec-fetch-user" to "?1",
                        "upgrade-insecure-requests" to "1"
                    )
                )

            val doc: Document? = Jsoup.parse(
                result
            )

            if (doc == null) {
                return null
            }

            val bigs = doc.select("span.img_block_big")

            if (bigs.size == 0) {
                return null
            }

            val big = bigs[(Math.random() * bigs.size).toInt()]

            val bigUrl = PREFIX_URL + big.select("a").attr("href")

            return getImgInfo(bigUrl);
        }

        /**
         * 获取图片详情页图片
         */
        fun getImgInfo(bigUrl: String): InputStream {

            println("访问大图地址: ${bigUrl}")
            val result =
                RestUtil.send(
                    bigUrl, null,
                    null, 0, String::class.java
                )

            val doc: Document? = Jsoup.parse(
                result.body
            )

            if (doc == null) {
                throw RuntimeException("未读取到内容")
            }

            val imgItem = doc.select("div#big_preview_cont");

            val img = PREFIX_URL + imgItem.select("a").attr("href")

            println("读取到图片地址: $img")

            val inputStream = FileNetUtil.getNetInputStream(img);

            return inputStream
        }

    }

}

fun main() {
    AnimeUtil.getAP("");

//    val key = "wc"
//    println(key.substring(key.length))
}